﻿

using System;
using System.Collections.Generic;
using System.IO;
using GameData;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace TNServer
{
    
    public class ExcelDataManager
    {
        private HSSFSheet sht;
        private string excelContent;

        public string url;
        public Dictionary<string, List<int>> vipHasDic = new Dictionary<string, List<int>>();

        /// <summary>
        /// key id
        /// </summary>
        public Dictionary<string,CustomerData> CustomerDatas = new Dictionary<string, CustomerData>(); 
        //


        public void AddCustomerData(CustomerData customerData,bool isnew=true)
        {
            if (CustomerDatas.ContainsKey(customerData.id))
            {

                //如果存在一样的ID，则有两张收款单据
                CustomerData temp = CustomerDatas[customerData.id];
                for (int i = 0; i < customerData.MoneyNumLists.Count; i++)
                {
                    string cs = customerData.MoneyNumLists[i];
                    if (!temp.MoneyNumLists.Contains(cs))
                    {
                        temp.MoneyNumLists.Add(cs);
                    }
                }

            }
            else
            {
                if (isnew)
                {
                    if (!Application.GlobalDb.GetCurGlobalVO().NewCustomerDatas.ContainsKey(customerData.id))
                    {
                        Application.GlobalDb.GetCurGlobalVO().NewCustomerDatas.Add(customerData.id, customerData);

                    }
                }
                else
                {
                    if (!Application.GlobalDb.GetCurGlobalVO().CustomerDatas.ContainsKey(customerData.id))
                    {
                        Application.GlobalDb.GetCurGlobalVO().CustomerDatas.Add(customerData.id, customerData);
                        int c = Application.GlobalDb.GetCurGlobalVO().numberCountdic[customerData.@group];
                        c += 1;
                        Application.GlobalDb.GetCurGlobalVO().numberCountdic[customerData.@group] = c;
                    }

                }

                CustomerDatas.Add(customerData.id,customerData);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        public List<CustomerData> GetCustomerDataByIdCrad(string card)
        {
            List<CustomerData> resDic = new List<CustomerData>();
            //
            foreach (var p in CustomerDatas)
            {
                //
                if (card.Length==6)
                {
                    string l6 = p.Value.IdCard;
                    if (l6.Length>=6)
                    {
                        l6 = l6.Substring(l6.Length - 6, 6);
                        if (l6.Equals(card))
                        {
                            resDic.Add(p.Value);
                        }
                    }


                }
                else
                {
                    if (p.Value.IdCard.Equals(card))
                    {
                        resDic.Add(p.Value);
                    }
                }
            }
            return resDic;
        } 
        //


        public List<CustomerData> GetCustomerDataByPayNum(string num)
        {
            List<CustomerData> resDic = new List<CustomerData>();
            List<string> numlist = null;
            foreach (var p in CustomerDatas)
            {
                numlist = p.Value.MoneyNumLists;
                for (int i = 0; i < numlist.Count; i++)
                {
                    string sp = numlist[i];
                    if (sp.Equals(num))
                    {
                        //resDic.Add(p.Value);
                        //添加有相同身份证的
                        //
                        List<CustomerData> pl = GetCustomerDataByIdCrad(p.Value.IdCard);
                        //
                        resDic.AddRange(pl);
                        break;
                    }
                }


            }

            return resDic;
        }
        //


        public void WriteExcelQue()
        {
            if (!File.Exists("预约排号.xls"))
            {
                return;
            }
            HSSFWorkbook wb = new HSSFWorkbook(new FileStream("预约排号.xls", FileMode.Open));
            sht = (HSSFSheet) wb.GetSheet("Sheet1");
            int rowCount = sht.PhysicalNumberOfRows;
            int colsCount = sht.GetRow(0).PhysicalNumberOfCells;
            Console.WriteLine("写入的excel数据行数=" + rowCount);
            if (rowCount < 1)
            {
                return;
            }
            //遍历所有客户
            for (int i = 1; i < rowCount; i++)
            {
                IRow row = sht.GetRow(i);
                string id = "";
                string paic="";
                ICell numCell = null;
                for (int j = 0; j < colsCount; j++)
                {
                    //
                    ICell topCell = sht.GetRow(0).GetCell(j);
                    string keyname = topCell.StringCellValue;

                    ICell cell = row.GetCell(j);
                    string value = "";
                    if (cell.CellType == CellType.NUMERIC)
                    {
                        value = cell.NumericCellValue.ToString();
                    }
                    else
                    {
                        value = cell.StringCellValue;
                    }

                    if (keyname.Contains("ID"))
                    {
                        id = value;
                    }
                    else if (keyname.Contains("已取号码"))
                    {
                        Console.WriteLine("已取号码" + j);

                        numCell = cell;
                    }
                    //

                }
                //
                if (numCell!= null)
                {
                    CustomerData customerData = Application.GlobalDb.GetCurGlobalVO().CustomerDatas[id];
                    if (customerData.NumerList.Count > 0)
                    {
                        Console.WriteLine(customerData.name+"客户取号:" + customerData.NumerList[0]);
                        numCell.SetCellValue(customerData.NumerList[0]);
                    }
                    else
                    {
                        Console.WriteLine(customerData.name + "客户没有取号" );

                        numCell.SetCellValue(-1);
                    }
                }

            }
            //
            //保存
            FileStream fs = new FileStream("预约排号.xls", FileMode.Open);
            wb.Write(fs);
            fs.Close();
        }

        /// <summary>
                /// 
                /// </summary>
        public  void LoadData()
        {
            if (!File.Exists("预约排号.xls"))
            {
                return;
            }
            FileStream fs = new FileStream("预约排号.xls", FileMode.Open);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            sht = (HSSFSheet)wb.GetSheet("Sheet1");
            int rowCount = sht.PhysicalNumberOfRows;
            int colsCount = sht.GetRow(0).PhysicalNumberOfCells;
            Console.WriteLine("读取的excel数据行数=" + rowCount);
            if (rowCount <1)
            {
                return;
            }


            //遍历所有客户
            for (int i = 1; i < rowCount; i++)
            {
                IRow row = sht.GetRow(i);
                CustomerData customerData = new CustomerData();

                for (int j = 0; j < colsCount; j++)
                {
                    ICell topCell = sht.GetRow(0).GetCell(j);
                    string keyname = topCell.StringCellValue;

                    ICell cell=row.GetCell(j);
                    
                    string value = "";
                    if (cell.CellType==CellType.NUMERIC)
                    {
                        value = cell.NumericCellValue.ToString();
                    }
                    else
                    {
                        value = cell.StringCellValue;
                    }

                    if (keyname.Contains("姓名"))
                    {
                        customerData.name = value;
                    }
                    else if (keyname.Contains("ID"))
                    {
                        customerData.id = value;
                    }
                    else if (keyname.Contains("身份证"))
                    {
                        customerData.IdCard = value;
                    }
                    else if (keyname.Contains("电话号码"))
                    {
                        customerData.phone = value.Replace("(M)", "");

                    }
                    else if (keyname.Contains("批次"))
                    {
                        customerData.group = value;
                        if (!vipHasDic.ContainsKey(value))
                        {
                            vipHasDic.Add(value,new List<int>());
                        }
                        Application.GlobalDb.GetCurGlobalVO().AddBatchlistItem(value);


                    }
                    else if (keyname.Contains("管理员")|| keyname.Contains("管理") || keyname.Contains("负责人"))
                    {
                        if (value.Contains("是") || value.Equals("1"))
                        {
                            customerData.isadmin = true;

                        }
                        else
                        {
                            customerData.isadmin = false;

                        }

                    }
                    else if (keyname.Contains("诚意金收据"))
                    {
                        customerData.MoneyNumLists.Add(value);
                    }
                    else if (keyname.Contains("VIP"))
                    {
                        if (value.Contains("是")|| value.Equals("1"))
                        {
                            customerData.isVip = true;

                        }
                        else
                        {
                            customerData.isVip = false;

                        }
                    }
                    else if (keyname.Contains("指定号码"))
                    {
                        int nul = -1;
                        bool re = int.TryParse(value,out nul);
                        if (re)
                        {
                            customerData.Number = nul;
                            
                        }
                        else
                        {
                            customerData.Number = -1;
                        }
                    }
                    else if (keyname.Contains("排位"))
                    {
                        //第几位抽中
                        customerData.que = value;
                    }
                    else if (keyname.Contains("已取号码"))
                    {
                        customerData.Number = int.Parse(value);

                    }
                    else if (keyname.Contains("置业顾问"))
                    {
                        customerData.salename = value;

                    }

                    //
                }
                customerData.groupTag = Application.GlobalDb.GetCurGlobalVO().GetGroupTag(customerData.@group);
                if (customerData.Number>0)
                {
                    vipHasDic[customerData.@group].Add(customerData.Number);
                }

                AddCustomerData(customerData,false);
            }
            //遍历
            foreach (var p in CustomerDatas)
            {

                Console.WriteLine(p.Value.@group+ p.Value.groupTag+" "+p.Value.name+" "+p.Value.IdCard+" "+p.Value.phone+" "+p.Value.Number);

            }
            fs.Close();
        }

    }
}